package com.photoeditor.demo.ui.dialog;

import android.content.Context;
import android.text.TextUtils;

import com.common.base.ComponentContext;
import com.photoeditor.demo.model.bean.face.BaseFaceResultBean;
import com.photoeditor.demo.model.preference.SPDataManager;
import com.photoeditor.demo.model.preference.SpConstant;
import com.photoeditor.demo.model.preference.SpUtils;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.ui.activity.face.FaceLogicActivity;

import java.lang.ref.WeakReference;

//import com.photoeditor.demo.ui.main.MainActivity;

/**
 * Created by mutour on 2017/1/17.
 * <p>
 * 评分引导展示时机:
 * 1.容器内,用户首次进入应用(新用户),使用过任意2次热门功能后(使用过表示进入到结果页;如果是付费功能,需要订阅成功后才算进入结果页)
 * 在第二次功能结果页点击任意按钮(包括物理返回键)时展示引导
 * 2.容器内,用户点击保存图片时,保存成功后弹出频分引导
 */

public class RateGuide {
    private static final int TYPE_NO_RATE = 0;
    private static final int TYPE_MAIN_RATE = 1;

    private static boolean sIsPushEntrance;

    public static final String ACTION_USE_FUNC = "ACTION_USE_FUNC";
    public static final String ACTION_USE_HOROSCOPE_TAB = "ACTION_USE_HOROSCOPE_TAB";
    public static final String ACTION_USE_HOROSCOPE_AND_FREE_FUN = "ACTION_USE_HOROSCOPE_AND_FREE_FUN";
    public static final String ACTION_CRYSTALL_BALL_TIME_THREE = "ACTION_CRYSTALL_BALL_TIME_THREE";
    public static final String ACTION_DEFALT = "ACTION_DEFALT";
    public static final String ACTION_NOTIFICAITON = "ACTION_NOTIFICAITON";


    public static final String ACTION_FROM_MAIN = "ACTION_FROM_MAIN";
    public static final String ACTION_USE_PALM = "ACTION_USE_PALM";
    public static final String ACTION_USE_FACE = "ACTION_USE_FACE";
    public static final String ACTION_READ_HOROSCOPE = "ACTION_READ_HOROSCOPE";

    public static final String INTENT_RATE_EFFECT_INFO = "INTENT_RATE_EFFECT_INFO";
    public static final String INTENT_RATE_EFFECT_BUNDLE = "INTENT_RATE_EFFECT_BUNDLE";

    private static WeakReference<OnRateChageListener> mOnRateChageListener;

    private static WeakReference<IDialog.IListener> mListener;

    @DialogStatistic.RateWhere
    private static int mWhere;

    public static void start(Context context, String action, OnRateChageListener rateChageListener,
                             IDialog.IListener listener, int where) {
        mOnRateChageListener = new WeakReference<>(rateChageListener);
        mListener = new WeakReference<>(listener);
        mWhere = where;
//        start(context, action, "");
        start0(context, action, "");
    }

    /**
     * 通知栏跳转特殊处理
     *
     * @param context
     * @param action
     * @param type
     * @param rateChageListener
     */
    public static void startFromNotification(Context context, String action, String type, OnRateChageListener rateChageListener) {
        mOnRateChageListener = new WeakReference<>(rateChageListener);
        start(context, action, type);
    }

    /**
     * 判断条件交给外面
     */
    private static void start0(Context context, String action, String type) {
        if (TextUtils.equals(ACTION_NOTIFICAITON, action)) {
            sIsPushEntrance = true;
        } else {
            sIsPushEntrance = false;
        }
        innerShowRateDialog(context, action, type);
        SPDataManager.setTodayHasRated();
    }

    private static void start(Context context, String action, String type) {
        if (SPDataManager.hasRated()) {//从未评分过才展示
            return;
        }
        if (!SPDataManager.getTodayHasRated()) {
            start0(context, action, type);
        }
    }

    private static void innerShowRateDialog(Context context, String action, String type) {
//        RateStrategyBean.RateStrategyBeanConfig config = getRateStrategyConfig();
//        if (config != null && RateStrategyBean.RateStrategyBeanConfig.SHOW.equals(config.getComFake())) {
//            new Builder().showVirtual(context,action,type);
//        } else {
        //new Builder().show(context);
        // 新样式
        new Builder().showVirtual(context, action, type);
//        }
    }


    public static class Builder {

        public boolean showVirtual(Context context, String action, String type) {
            if (context == null) {
                context = ComponentContext.getContext();
            }
            VirtualRateDialog.showVirtualRateDialog(context);
            saveExitRateDialogTime();
            return false;
        }

    }

    public static WeakReference<OnRateChageListener> getOnRateChageListener() {
        return mOnRateChageListener;
    }

    public static WeakReference<IDialog.IListener> getListener() {
        return mListener;
    }

    @DialogStatistic.RateWhere
    public static int getWhere() {
        return mWhere;
    }

    private static void saveExitRateDialogTime() {
        int shownTimes = SpUtils.obtain().getInt(SpConstant.RATE_DIALOG_SHOWN_TIMES, 0);
        SpUtils.obtain().save(SpConstant.RATE_DIALOG_SHOWN_TIMES, shownTimes + 1);

        SpUtils.obtain().save(SpConstant.RATE_DIALOG_LAST_SHOWN_TIME, System.currentTimeMillis());
    }


    protected static String getEntrance(Context context) {
        String entrance = StatisticsConstant.STATISTIC_EVENT_FUN_VALUE_OTHER;

        if (sIsPushEntrance) {
            entrance = StatisticsConstant.STATISTIC_EVENT_FEATURE_VALUE_ENTRANCE_PUSH;
            return entrance;
        }

//        if (context instanceof MainActivity){
//            entrance = StatisticsConstant.STATISTIC_EVENT_FEATURE_VALUE_ENTRANCE_HOROSCOPE;
//        }
//        else if (context instanceof CrystalBallActivity){
//            entrance = StatisticsConstant.STATISTIC_EVENT_FEATURE_VALUE_ENTRANCE_CRYSTALBALL;
//        } else if (context instanceof LuckActivity){
//            entrance = StatisticsConstant.STATISTIC_EVENT_FEATURE_VALUE_ENTRANCE_LUCKYNUM;
//        } else if (context instanceof CompatibilityResultActivity){
//            int type = ((CompatibilityResultActivity) context).getIconType();
//            if (type == CompatibilityActivity.TYPE_HOROSCOPE){
//                entrance = StatisticsConstant.STATISTIC_EVENT_FEATURE_VALUE_ENTRANCE_ZODIACCOMP;
//            } else {
//                entrance = StatisticsConstant.STATISTIC_EVENT_FEATURE_VALUE_ENTRANCE_LOVECOMP;
//            }
//        }
        else if (context instanceof FaceLogicActivity) {
            int type = ((FaceLogicActivity) context).getType();
            switch (type) {
                case BaseFaceResultBean.REPORT_TYPE_FACE_AGING:
                    entrance = StatisticsConstant.STATISTIC_EVENT_FEATURE_VALUE_FEATURE_FACE_AGING;
                    break;
                case BaseFaceResultBean.REPORT_TYPE_EXOTIC:
                    entrance = StatisticsConstant.STATISTIC_EVENT_FEATURE_VALUE_FEATURE_FACE_SWAP;
                    break;
                case BaseFaceResultBean.REPORT_TYPE_GENDER_SWITCH:
                    entrance = StatisticsConstant.STATISTIC_EVENT_FEATURE_VALUE_FEATURE_GENDER_SWITCH;
                    break;
                default:
            }
        }
//        else if (context instanceof BaseActicleDetailActivity){
//            entrance = StatisticsConstant.STATISTIC_EVENT_FUN_VALUE_ARTICLE;
//        } else if(context instanceof PalmResultActivity){
//            entrance = StatisticsConstant.STATISTIC_EVENT_FUN_VALUE_PALM;
//        }
        return entrance;
    }

    public static String getString(int id) {
        return ComponentContext.getContext().getResources().getString(id);
    }

    public interface OnRateChageListener {
        void onDisimiss();
    }

}
